package com.toy.core.security.chain;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.google.common.collect.ImmutableSet;
import com.toy.core.util.UrlUtils;

public class WebExcludeUrlSuffix {
	
	public final static String CONFIG = "classpath:/config/filter_exclude_suffix.properties";
	
	private static ImmutableSet<Object> excludedSuffix;
	
	static{
		try {
			Resource resource = new DefaultResourceLoader().getResource(CONFIG);
			Properties properties = new Properties();
			PropertiesLoaderUtils.fillProperties(properties, resource);
			excludedSuffix = ImmutableSet.copyOf(properties.keySet());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 判断URL是否web资源，如js/css/images
	 * @param request
	 * @return
	 */
	public static boolean isWebResourceUrl(HttpServletRequest request){
		String queryString = UrlUtils.getFullRequestUrl(request);
		String[] queryArray = queryString.split("\\.");
		if ( queryArray == null || queryArray.length == 1){
			return false;
		}else{
			String suffix = "." + queryArray[queryArray.length-1];
			return excludedSuffix.contains(suffix);
		}
	}
}
